package redis_client

import (
	"context"

	"gitee.com/huangxinbo/xcas/internal/config"
	"gitee.com/huangxinbo/xcas/pkg/logger"
	"github.com/redis/go-redis/v9"
)

// NewRedis creates Redis client
func NewRedis(cfg *config.Config, logger *logger.Logger) (*redis.Client, error) {
	client := redis.NewClient(&redis.Options{
		Addr:     cfg.Redis.Addr,
		Username: cfg.Redis.Username,
		Password: cfg.Redis.Password,
		DB:       cfg.Redis.DB,
	})

	ctx := context.Background()

	// Test connection
	_, err := client.Ping(ctx).Result()
	if err != nil {
		logger.Error("Redis connection failed: " + err.Error())
		return nil, err
	}

	return client, nil
}
